package com.example.yqand4v1_1.service;

import com.example.yqand4v1_1.dao.VolunteerEntityMapper;
import com.example.yqand4v1_1.dao.entity.Volunteer;
import com.example.yqand4v1_1.utils.SnowflakeIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class VolunteerService{

    @Autowired
    private VolunteerEntityMapper volunteerEntityMapper;

    private static int machineId = 1;
    public Map<String, Object> login(Volunteer volunteer) {
        //根据用户名和密码查询
        Volunteer loginVolunteer = volunteerEntityMapper.selectOne(volunteer);
        //token
        if(loginVolunteer != null){
            //UUID
            String key = "user:"+ UUID.randomUUID();
            //返回数据
            Map<String,Object> data = new HashMap<>();
            data.put("token",key);
            data.put("id",loginVolunteer.getId());
            data.put("name",loginVolunteer.getName());
            data.put("sex",loginVolunteer.getSex());
            data.put("phone_num",loginVolunteer.getPhoneNum());
            data.put("birth",loginVolunteer.getBirth());
            data.put("location",loginVolunteer.getLocation());
            data.put("university_name",loginVolunteer.getUniversityName());
            data.put("description",loginVolunteer.getDescription());
            return data;
        }
        return null;
    }



    public Map<String, Object> changeUserInfo(Volunteer volunteer) {
        if(volunteer.getSex().equals("1001")){
            volunteer.setSex("女");
        }else {
            volunteer.setSex("男");
        }
        volunteerEntityMapper.updateById(volunteer);
        Map<String, Object> data = new HashMap<>();
        //UUID生成token
        String key = "user:" + UUID.randomUUID();
        data.put("token", key);
        data.put("id", volunteer.getId());
        data.put("name", volunteer.getName());
        data.put("sex", volunteer.getSex());
        data.put("phone_num", volunteer.getPhoneNum());
        data.put("birth", volunteer.getBirth());
        data.put("location", volunteer.getLocation());
        data.put("university_name", volunteer.getUniversityName());
        data.put("description", volunteer.getDescription());
        return data;
    }

    public Map<String, Object> register(Volunteer volunteer) {
        if(volunteer.getSex() == null){}
        else {
        if(volunteer.getSex().equals("1001")){
            volunteer.setSex("女");
        }else{
            volunteer.setSex("男");
        }
        }
        SnowflakeIDGenerator idGenerator = new SnowflakeIDGenerator(machineId++); // Machine ID
        int bitLimit = 7; // 限制ID的位数
        long id = idGenerator.generateIDWithBitLimit(bitLimit);
        volunteer.setId(Integer.valueOf(""+volunteer.getId()+id));
//        System.out.println(volunteer.getId());
        volunteerEntityMapper.insertToUser(volunteer);
        int result = volunteerEntityMapper.insert(volunteer);
      //  System.out.println(result);
        if(result > 0){
            volunteer.setPassword(""); //清除密码
            Map<String, Object> data = new HashMap<>();
            data.put("id", volunteer.getId());
            return data;
        }
        else {
            return null;
        }
    }

    public Boolean changePsw(Volunteer volunteer) {
         Volunteer update = new Volunteer();
         update.setId(volunteer.getId());
         update.setPassword(volunteer.getPassword());
         int result = volunteerEntityMapper.updateById(update);
         if(result > 0) {
             return true;
         }else {
             return false;
         }
    }

    public Boolean checkInfo(Volunteer volunteer) {
        Volunteer query = volunteerEntityMapper.checkUser(volunteer);
       // System.out.println(query);
        String type = volunteer.getDescription();
        String[] parts = type.split("/");
        if(parts[0].equals("1") ){
            if(!volunteer.getName().equals(query.getName())){return false;}
        }else {
            if(!volunteer.getBirth().equals(query.getBirth())){return false;}
        }
        if(parts[1].equals("3")){
            if(!volunteer.getUniversityName().equals(query.getUniversityName())){return false;}
        }else {
            if(!volunteer.getLocation().equals(query.getLocation())){return false;}
        }
         return true;
    }
}
